import useUserStore from "../../store/user";
import { request } from "../../utils/request";
import { ElMessage } from "element-plus";

// 用户登录请求封装
export const reqUserLogin=async (loginInfo)=>{
  let res=-1;
  let response=await request.post("/api/lab/admin/v1/user/login",loginInfo).then((data)=>{
    let userInfo={
      userName:loginInfo.username,
      token:data.data.token,
    }
    useUserStore().login(userInfo)
    ElMessage({
      message:'登录成功',
      grouping:true,
      type: 'success'
    })
    res = 0
  }).catch((err)=>{
    if(err.message=="A000203"){
      useUserStore().logout()
      ElMessage({
        message:'登录信息过期,准备前往登录界面',
        type: 'error',
        grouping:true
      })
      res = 1
    }
    else{
      ElMessage({
        message:err.message,
        type: 'error',
        grouping:true
      })
      res = 2
    }
  })
  return res
}
// 首页数据请求封装
export const reqHomeData=async ()=>{
  let res={
    code: -1,
    data: null
  }
  let response=await request.get("/api/lab/admin/v1/user/info").then((data)=>{
    res.code = 0
    res.data=data.data
    useUserStore().updateType(res.data.userType)
  }).catch((err)=>{
    if(err.message=="A000203"){
      useUserStore().logout()
      ElMessage({
        message:'登录信息过期,准备前往登录界面',
        grouping:true,
        type: 'error'
      })
      res.code = 1
    }
    else{
      ElMessage({
        message:err.message,
        grouping:true,
        type: 'error'
      })
      res.code = 2
    }
  })
  return res
}